package com.china.controller;


import com.china.service.IChatHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {

    @Autowired
    private  ChatClient chatClient;
    @Autowired
    private IChatHistoryService chatHistoryService;

    @GetMapping(value = "/chat", produces = "text/html;charset=UTF-8")
    public Flux<String> stream( String prompt,String chatId) {
        chatHistoryService.saveChatHistory("chat",chatId);
        return chatClient
                .prompt(prompt) // 传入user提示词
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, chatId))
                .stream() // 同步请求，会等待AI全部输出完才返回结果
                .content(); //返回响应内容
    }
}